package com.example.qwertie;

import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class MenuActivity extends Activity {

	public static final String PREFS_NAME = "MyPrefsFile";
	public static final String levelProgressTag = "levelProgress";
	
	
	boolean musicFlag = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        
     // get the level progress (levels are unblocked increasingly)
        SharedPreferences sharedPrefs = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor sharedPrefsEditor = sharedPrefs.edit();
        if(sharedPrefs.getLong(levelProgressTag, 0) == 0) {
        	Log.i("QWERTIE", "init levelProgress");
        	sharedPrefsEditor.putLong(levelProgressTag, 2);
        	sharedPrefsEditor.commit();
        }
        long levelProgress = sharedPrefs.getLong(levelProgressTag, 0);
        Log.i("QWERTIE", "levelProgress = "+levelProgress);
        
        // enable/disable buttons
        enableButtons(levelProgress);
        ((Button)findViewById(R.id.button_settings)).setEnabled(false);
        
        musicFlag = false;
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.menumusic);
        Thread musicThread = new Thread(){
        	public void run(){
        		mp.start();
        		try{
        			while(true){
        				if(musicFlag==true)
        					throw new InterruptedException();
        			}
        		}
        		catch(InterruptedException e)
        		{
        			mp.stop();
        			mp.release();
        		}
        	}
        };
        musicThread.start();
        
    }
    
    @Override
    protected void onPause() {
    	musicFlag = true;
    	super.onPause();
    }
    
    @Override
    protected void onDestroy() {
    	musicFlag = true;
    	super.onDestroy();
    }

    private void enableButtons(long levelProgress) {
		List<Button> level_btns = new ArrayList<Button>();
		level_btns.add((Button)findViewById(R.id.button_level1));
		level_btns.add((Button)findViewById(R.id.button_level2));
		level_btns.add((Button)findViewById(R.id.button_level3));
		level_btns.add((Button)findViewById(R.id.button_level4));
		level_btns.add((Button)findViewById(R.id.button_level5));
		level_btns.add((Button)findViewById(R.id.button_level6));
		level_btns.add((Button)findViewById(R.id.button_level7));
		level_btns.add((Button)findViewById(R.id.button_level8));
		level_btns.add((Button)findViewById(R.id.button_level9));
		level_btns.add((Button)findViewById(R.id.button_level10));
		level_btns.add((Button)findViewById(R.id.button_level11));
		level_btns.add((Button)findViewById(R.id.button_level12));
		
		for(int i=(int)levelProgress; i<level_btns.size(); i++) {
			level_btns.get(i).setEnabled(false);
		}
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      // ignore orientation/keyboard change
      super.onConfigurationChanged(newConfig);
    }
    
    public void onClick(View view) {
    	switch(view.getId()) {
    	case R.id.button_level1:
    		startGame(1);
    		break;
    	case R.id.button_level2:
    		startGame(2);
    		break;
    	case R.id.button_level3:
    		startGame(3);
    		break;
    	case R.id.button_level4:
    		startGame(4);
    		break;
    	case R.id.button_level5:
    		startGame(5);
    		break;
    	case R.id.button_level6:
    		startGame(6);
    		break;
    	case R.id.button_level7:
    		startGame(7);
    		break;
    	case R.id.button_level8:
    		startGame(8);
    		break;
    	case R.id.button_level9:
    		startGame(9);
    		break;
    	case R.id.button_level10:
    		startGame(10);
    		break;
    	case R.id.button_level11:
    		startGame(11);
    		break;
    	case R.id.button_level12:
    		startGame(12);
    		break;
    	case R.id.button_quit:
    		quit();
    		break;
    	}
    }
    
    public void startGame(long l) {
    	musicFlag = true;
    	Intent intent = new Intent(this, GameActivity.class);
    	intent.putExtra(GameActivity.levelTag, l);
    	startActivity(intent);
    }
    
    @Override
    public void onBackPressed() {
       quit();
    }
    
    public void quit() {
    	musicFlag = true;
    	Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
    }
}
